package com.useeinfo.oa.open.api.taskOrder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.common.utils.NumberUtil;
import com.useeinfo.oa.modules.task.biz.TaskOrderBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/taskOrderCharts")
public class TaskOrderChartsAPI {

    @Autowired
    private TaskOrderBiz taskOrderBiz;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @RequestMapping("/getOrderData")
    public JSONObject getOrderData(String type, String enterpriseId) {
        JSONObject result = new JSONObject();
        JSONArray dateTimes = new JSONArray();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONArray totalArray = new JSONArray();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date startDateTime = new Date();
        Date endDateTime = new Date();
        Date now = new Date();
        List<String> newDates = new ArrayList<>();
        JSONArray allOrders = new JSONArray();
        JSONArray completedOrders = new JSONArray();
        if ("month".equals(type)) {
            startDateTime = c.getTime();
            endDateTime = now;
            newDates = DateTimeUtils.getDates(startDateTime, endDateTime, "M/d");
        } else if ("week".equals(type)) {
            startDateTime = DateTimeUtils.getFirstDateOfWeek(now);
            endDateTime = now;
            newDates = DateTimeUtils.getDates(startDateTime, endDateTime, "MM-dd");
        } else if ("quarter".equals(type)) {
            int season = DateTimeUtils.getSeason(now);
            for (int i = 1; i <= season; i++) {
                newDates.add(NumberUtil.convert(i) + "季度");
                allOrders = taskOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, false);
                completedOrders = taskOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, true);
                int allTotal = 0;
                int allComplete = 0;
                if (allOrders.size() > 0) {
                    for (int a = 0; a < allOrders.size(); a++) {
                        JSONArray completeObject = completedOrders.getJSONArray(a);
                        JSONArray allObject = allOrders.getJSONArray(a);
                        allTotal  += allObject.getIntValue(1);
                        allComplete += completeObject.getIntValue(1);
                    }
                }
                if (allTotal == 0) {
                    jsonArray.add(0);
                } else {
                    jsonArray.add((allComplete * 100 / allTotal * 100) / 100);
                }
                totalArray.add(allTotal);
                dateTimes.addAll(newDates);
            }
        }

        if (!"quarter".equals(type)) {
            dateTimes.addAll(newDates);
            allOrders = taskOrderBiz.getbetween(enterpriseId, DateTimeUtils.formatDateToStringWithTime(startDateTime), DateTimeUtils.formatDateToStringWithTime(endDateTime), 2, false);
            completedOrders = taskOrderBiz.getbetween(enterpriseId, DateTimeUtils.formatDateToStringWithTime(startDateTime), DateTimeUtils.formatDateToStringWithTime(endDateTime), 2, true);

            if (allOrders.size() > 0) {
                for (int i = 0; i < allOrders.size(); i++) {
                    JSONArray completeObject = completedOrders.getJSONArray(i);
                    JSONArray allObject = allOrders.getJSONArray(i);
                    int total = allObject.getIntValue(1);
                    int complete = completeObject.getIntValue(1);
                    if (total == 0) {
                        jsonArray.add(0);
                    } else {
                        jsonArray.add((complete * 100 / total * 100) / 100);
                    }
                    totalArray.add(total);
                }
            }
        }


        jsonObject1.put("data", totalArray);
        jsonObject1.put("barWidth", 10);
        jsonObject1.put("name", "Others");
        jsonObject1.put("type", "bar");

        jsonObject2.put("data", jsonArray);
        jsonObject2.put("name", "completion rate（%）");
        jsonObject2.put("type", "line");
        jsonObject2.put("yAxisIndex", 1);

        result.put("0", jsonObject1);
        result.put("1", jsonObject2);

        return result;
    }

    /**
     * 班组人员接单数
     * @param organizationId
     * @param start
     * @param end
     * @return
     */
    @RequestMapping("/getOrganizationReceiveNum")
    public JSONArray getOrganizationReceiveNum(String organizationId, String start, String end) {
        List<Object> objects=taskOrderBiz.getOrganizationReceiveNum(organizationId, start, end);
        JSONArray nowData = new JSONArray();
        objects.forEach(y -> {
            nowData.add(net.sf.json.JSONArray.fromObject(y).getInt(0));
        });
        return nowData;

    }

    /**
     *  部门维修人员工时统计
     */
    @RequestMapping("/getHoursData")
    public JSONArray getHoursChartsData(String startDateTime, String endDateTime, String organizationId,String enterpriseId){
        return taskOrderBiz.getHoursChartsData(startDateTime,endDateTime,organizationId,enterpriseId);
    }

}
